﻿using System.Collections.Generic;
using System.IO;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace WindowsGame1
{
    public class GameWorldScreen : IGameScreen
    {
        private Texture2D idle;
        private Texture2D run;
        private Texture2D jump;
        private Texture2D idle2;
        private Texture2D jump2;
        private Texture2D run2;

        private AnimatedSprite _hero1;
        private AnimatedSprite _hero2;
        private Texture2D _shit;

        private KeyboardState oldState;

        private Texture2D blockTexture1;
        private Texture2D blockTexture2;
        private int currentLevel;
        private List<Wall> walls;

        private SpriteFont _menuFont;

        private static int ScrollX;
        private int levelLength;

        public int Width { get; private set; }
        public int Height { get; private set; }

        private ContentManager _contentManager;

        private CowboyGame _cowboyGame;

        public bool CollidesWithLevel(Rectangle rect)
        {
            foreach (Wall wall in walls)
            {
                if (wall.rect.Intersects(rect))
                    return true;
            }
            return false;
        }
        
        public GameWorldScreen(int width, int height, ContentManager contentManager, CowboyGame cowboyGame)
        {
            Width = width;
            Height = height;
            _contentManager = contentManager;
            _cowboyGame = cowboyGame;
            _menuFont = contentManager.Load<SpriteFont>("MenuFont");
            idle = _contentManager.Load<Texture2D>("Textures/Idle");
            run = _contentManager.Load<Texture2D>("Textures/run");
            jump = _contentManager.Load<Texture2D>("Textures/Jump");
            run2 = _contentManager.Load<Texture2D>("Textures/run2");
            jump2 = _contentManager.Load<Texture2D>("Textures/Jump2");
            idle2 = _contentManager.Load<Texture2D>("Textures/Idle2");
            blockTexture1 = _contentManager.Load<Texture2D>("Textures/block");
            blockTexture2 = _contentManager.Load<Texture2D>("Textures/block2");
            _shit = _contentManager.Load<Texture2D>("Textures/BullShit");

            
            _hero1 = new AnimatedSprite(new Rectangle(0, Height - idle.Height - 40, 60, 60), idle, run, jump, this);
            _hero2 = new AnimatedSprite(new Rectangle(Width-idle.Width,Height-idle.Height-40,60,60), idle2,run2,jump2,this);
            CreateLevel();
            // TODO: use this.Content to load your game content here
        }

        public void Update(GameTime gameTime, KeyboardState keyboardState)
        {
            if (keyboardState.IsKeyDown(Keys.Escape) && oldState.IsKeyUp(Keys.Escape) )
            {
                CowboyGame.CurrentScreen = Screens.MenuScreen;
                _cowboyGame.SuppressDraw();

            }

            if (keyboardState.IsKeyDown(Keys.P) && oldState.IsKeyUp(Keys.P))
            {
                CowboyGame.IsPaused = !CowboyGame.IsPaused;
                
            }

            if (CowboyGame.IsPaused)
            {
                oldState = keyboardState;
                return;
            }

            if (keyboardState.IsKeyDown(Keys.Space) && oldState.IsKeyUp(Keys.Space))
                CreateLevel();


            // TODO: Add your update logic here
            
            if (keyboardState.IsKeyDown(Keys.Left))
                _hero1.StartRun(false);
            else if (keyboardState.IsKeyDown(Keys.Right))
                _hero1.StartRun(true);
            else
                _hero1.Stop();

            if (keyboardState.IsKeyDown(Keys.Up))
                _hero1.Jump();

            Rectangle hero1ScreenRect = GetScreenRect(_hero1.Rect);

            if (keyboardState.IsKeyDown(Keys.A))
                _hero2.StartRun(false);
            else if (keyboardState.IsKeyDown(Keys.D))
                _hero2.StartRun(true);
            else
                _hero2.Stop();

            if (keyboardState.IsKeyDown(Keys.W))
                _hero2.Jump();

            Rectangle hero2ScreenRect = GetScreenRect(_hero2.Rect);

            if (((hero1ScreenRect.Left < 0) && (hero2ScreenRect.Right < Width)) || 
                ((hero2ScreenRect.Left < 0) && (hero1ScreenRect.Right < Width)))
                Scroll(-3 * gameTime.ElapsedGameTime.Milliseconds / 10);
            else if (((hero1ScreenRect.Right > Width) && (hero2ScreenRect.Left > 0)) || 
                ((hero2ScreenRect.Right > Width) && (hero1ScreenRect.Left > 0)))
                Scroll(3 * gameTime.ElapsedGameTime.Milliseconds / 10);


            oldState = keyboardState;
            _hero1.Update(gameTime);
            _hero2.Update(gameTime);
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Begin();
            if (CowboyGame.IsPaused)
            {
                spriteBatch.DrawString(_menuFont, "Paused", new Vector2(Width/2,Height/2), Color.Red);
            }
            
            foreach (Wall wall in walls)
            {
                wall.Draw(spriteBatch);
            }
            spriteBatch.End();
            _hero2.Draw(spriteBatch);
            _hero1.Draw(spriteBatch);
        }

        public void Scroll(int dx)
        {
            if (ScrollX + dx >= 0 && ScrollX + dx <= levelLength - 400)
                ScrollX += dx;
        }

        public static Rectangle GetScreenRect(Rectangle rect)
        {
            Rectangle screenRect = rect;
            screenRect.Offset(-ScrollX, 0);
            return screenRect;
        }

        public void CreateLevel()
        {
            currentLevel++;
            if (currentLevel > 3)
                currentLevel = 1;
            string[] s = File.ReadAllLines("content/levels/level" + currentLevel + ".txt");

            levelLength = 40 * s[0].Length;
            walls = new List<Wall>();
            int x = 0;
            int y = 0;
            foreach (string str in s)
            {
                foreach (char c in str)
                {
                    Rectangle rect = new Rectangle(x, y, 40, 40);
                    if (c == 'X')
                    {
                        Wall wall = new Wall(rect, blockTexture1);
                        walls.Add(wall);
                    }
                    if (c == 'Y')
                    {

                        Wall wall = new Wall(rect, blockTexture2);
                        walls.Add(wall);
                    }
                    x += 40;
                }
                x = 0;
                y += 40;
            }

        }
    }
}